#include "storage.h"
#include "sensor_check/sensor.h"
#include "../core/uart/uart.h"
#include <nvs_flash.h>

// NVS初始化成功标识
bool Storage::nvs_state = false;

nvs_handle_t config;

// init nvs
void Storage::nvs_init()
{
    nvs_flash_init();
}

// Int32读写 写入需要 value 默认写入 0
int32_t Storage::nvs_int32(const char *name, int32_t *vault, bool read)
{
    if (read)
        return nvs_get_i32(config, name, vault);
    else
        return (int32_t)nvs_set_i32(config, name, *vault);
}

// 启动时加载脚本
void Storage::nvs_setup_read()
{
    nvs_open("Config", NVS_READONLY, &config);

    for (uint8_t i = 0; i < STORAGE; i++)
    {
        nvs_int32(storage_direct[i], &sensor.touch_bar[i].x);
    }
    for (uint8_t i = 0; i < STORAGE; i++)
    {
        nvs_int32(storage_direct[i + STORAGE], &sensor.touch_bar[i].y);
    }
    for (uint8_t i = 0; i < TOUCH_NUM; i++)
    {
        int32_t rd[2] = {0};
        nvs_int32(storage_maiserial[i], &rd[0]);
        nvs_int32(storage_maiserial[i + (TOUCH_NUM)], &rd[1]);
        if (rd[0] < 0 || rd[0] > 35)
            rd[0] = 1;
        if (rd[1] < 0 || rd[1] > 35)
            rd[1] = 2;
        sensor.mai_map[i][0] = rd[0];
        sensor.mai_map[i][1] = rd[1];
    }
    int32_t speed = DEFAULT_SERIAL_BAUD;
    nvs_int32(storage_direct[118], &speed); // uart速度
    if (speed == 9600 || speed == 115200 || speed == 250000 || speed == 500000 || speed == 1000000 || speed == 1500000 || speed == 2000000)
    {
        uart.uart_speed = speed;
    }

    for (uint8_t a = 0; a < TOUCH_NUM; a++)
    {
        int32_t data = TOUCH_SPL_DEF;
        nvs_int32(touch_store[a], &data);
        sensor.nvs_touch_spl_set(a, (int8_t)data);
    }
    // 固件设置
    {   
        // 固件延迟
        int32_t time = 0;
        nvs_int32("TIMEDELAY", &time);
        core.touch_delaytime = time & 0xFFFF;
        // 采样时间
        int32_t sample_time = 0;
        nvs_int32("SAMPLETIME", &sample_time);
        sensor.sample_time = sample_time & 0xFFFF;
        // 采样阈值锁定
        int32_t _touch_refence_lock = 0;
        nvs_int32("TOUCHREFLOCK", &_touch_refence_lock);
        sensor.touch_refence_lock = _touch_refence_lock;
    }
    nvs_close(config);
}

// 保存脚本
void Storage::nvs_storage()
{
    nvs_open("Config", NVS_READWRITE, &config);
    for (uint8_t i = 0; i < STORAGE; i++)
    {
        nvs_int32(storage_direct[i], &sensor.touch_bar[i].x, false);
    }
    for (uint8_t i = 0; i < STORAGE; i++)
    {
        nvs_int32(storage_direct[i + STORAGE], &sensor.touch_bar[i].y, false);
    }
    for (uint8_t i = 0; i < TOUCH_NUM; i++)
    {
        nvs_int32(storage_maiserial[i], &sensor.mai_map[i][0], false);
        nvs_int32(storage_maiserial[i + (TOUCH_NUM)], &sensor.mai_map[i][1], false);
    }
    nvs_int32(storage_direct[118], &uart.uart_speed, false); // uart速度
    for (uint8_t a = 0; a < TOUCH_NUM; a++)
    {
        int32_t data = (uint32_t)sensor.nvs_touch_spl_read(a);
        nvs_int32(touch_store[a], &data, false);
    }
    // 固件设置
    {   
        // 固件延迟
        int32_t time = core.touch_delaytime;
        nvs_int32("TIMEDELAY", &time, false);
        // 采样时间
        int32_t sample_time = sensor.sample_time;
        nvs_int32("SAMPLETIME", &sample_time, false);
        // 采样阈值锁定
        int32_t _touch_refence_lock = sensor.touch_refence_lock;
        nvs_int32("TOUCHREFLOCK", &_touch_refence_lock, false);
    }
    nvs_commit(config);
    nvs_close(config);
    uart.send("INFO:STORE_SETTINGS_OK");
}